// Code copyright Excitation Ltd 2007
using System;
using System.Collections;

namespace CopyUserQuery
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class Class1
	{
		static Hashtable htArgs;
		[STAThread]
		static void Main(string[] args)
		{
			try
			{
				ReadArgs(args);
				CopyQuery cq = new CopyQuery(GetArgString("url"), GetArgString("owner"));
				Guid[] users = new Guid[0];
				string[] userNames = GetArg("users") as string[];
				if (userNames != null)
					users = cq.GetUserIds(userNames);
				else
					users = cq.GetAllUsers();
				string viewName = GetArgString("viewname");
				int count = cq.DoClone(viewName, users);
				Console.WriteLine(viewName + ": " + count.ToString() + " views created");
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.Message);
			}
		}
		static string GetArgString(string Name)
			{return GetArgString(Name, null);}	
		static string GetArgString(string Name, string DefaultValue)
			{return GetArg(Name, true, DefaultValue) as string;}	
		static object GetArg(string Name)
			{return GetArg(Name, false);}	
		static object GetArg(string Name, bool ConvertArray)
			{return GetArg(Name, ConvertArray, null);}	
		static object GetArg(string Name, bool ConvertArray, object DefaultValue)
		{
			object ret = DefaultValue;
			if (htArgs.ContainsKey(Name))
			{
				ret = htArgs[Name];
				if (ConvertArray && ((string[]) ret).Length == 1)
					ret = ((string[]) ret)[0];
			}
			return ret;
		}
		static void ReadArgs(string[] args)
		{
			htArgs = new Hashtable(args.Length);
			foreach(string arg in args)
			{
				string[] splitEq = arg.Split("=".ToCharArray());
				if (splitEq.Length >= 2)
				{
					string[] splitList = splitEq[1].Split(";".ToCharArray());
					htArgs.Add(splitEq[0].ToLower(), splitList);
				}
			}
		}

	}
}
